<template>
    <div class="sidebar">
        <el-menu :default-active="onRoutes" class="el-menu-vertical-demo" @open="handleOpen"
                 @close="handleClose" theme="dark" unique-opened router>
            <template v-for="item in items">
                <template v-if="item.children">
                    <el-submenu :index="item.code">
                        <template slot="title"><i :class="item.icon"></i>{{ item.name }}</template>
                        <el-menu-item v-for="(subItem,i) in item.children" :key="i" :index="subItem.code">{{ subItem.name }}
                        </el-menu-item>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.code">
                        <i :class="item.icon"></i>{{ item.name }}
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                items: [
                ]
            }
        },
        computed:{
            onRoutes(){
                return this.$route.path.replace('/','');
            }
        },
        created(){
            const self = this;
            // 根据权限获取菜单
            self.$axios.get("http://localhost:1220/getMenusByRoleIds")
            .then(function (response) {
                console.info(response);
                if(response.data.data==null){
                    self.$message({
                        message: response.data.msg,
                        type: 'error'
                    });
                    self.$router.push('/login');
                    return;
                }
                self.items=response.data.data;
            })
            .catch(function (error) {
                console.log(error);
                if (error.response) {
                    self.message = error.response.data.msg;
                } else {
                    console.log('error', error.message);
                    self.$message({
                        message: "网络异常",
                        type: 'error'
                    });
                }
            });
        },
        methods:{
            handleOpen: function (key, keyPath) {
                console.log(key, keyPath);
            },
            handleClose: function (key, keyPath) {
                console.log(key, keyPath);
            }
        }
    }
</script>

<style scoped>
    .sidebar{
        display: block;
        position: absolute;
        width: 250px;
        left: 0;
        top: 70px;
        bottom:0;
        background: #2E363F;
    }
    .sidebar > ul {
        height:100%;
    }
</style>
